﻿using CreateCodeByDatabase.Model;
using MySql.Data.MySqlClient;
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CreateCodeByDatabase.Dal
{
    class DapperHelper : IDisposable
    {
        private enumConnectionType connectionType;
        private static string _OracleConnectionInfo;
        /// <summary>
        /// Oracle 数据库连接字符串
        /// </summary>
        private static string OracleConnectionInfo
        {
            get
            {
                if (string.IsNullOrWhiteSpace(_OracleConnectionInfo))
                {
                    ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["oracle"];
                    if (settings != null)
                        _OracleConnectionInfo = settings.ConnectionString;
                    if (string.IsNullOrWhiteSpace(_OracleConnectionInfo))
                        throw new Exception("Oracle数据库连接字符串异常");
                }
                return _OracleConnectionInfo;
            }
        }
        private static string _SqlServerConnectionInfo;
        /// <summary>
        /// SqlServer 数据库连接字符串
        /// </summary>
        private static string SqlServerConnectionInfo
        {
            get
            {
                if (string.IsNullOrWhiteSpace(_SqlServerConnectionInfo))
                {
                    ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["sqlServer"];
                    if (settings != null)
                        _SqlServerConnectionInfo = settings.ConnectionString;
                    if (string.IsNullOrWhiteSpace(_SqlServerConnectionInfo))
                        throw new Exception("SqlServer数据库连接字符串异常");
                }
                return _SqlServerConnectionInfo;
            }
        }
        private static string _MySqlConnectionInfo;
        /// <summary>
        /// SqlServer 数据库连接字符串
        /// </summary>
        private static string MySqlConnectionInfo
        {
            get
            {
                if (string.IsNullOrWhiteSpace(_MySqlConnectionInfo))
                {
                    ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["MySql"];
                    if (settings != null)
                        _MySqlConnectionInfo = settings.ConnectionString;
                    if (string.IsNullOrWhiteSpace(_MySqlConnectionInfo))
                        throw new Exception("MySql数据库连接字符串异常");
                }
                return _MySqlConnectionInfo;
            }
        }
        private IDbConnection _DbContext;
        public DapperHelper(enumConnectionType type = enumConnectionType.SqlServer)
        {
            connectionType = type;
        }
        public IDbConnection DbContext
        {
            get
            {
                if (_DbContext == null)
                {
                    if (connectionType == enumConnectionType.Oracle)
                        _DbContext = GetOracleConnection();
                    else if (connectionType == enumConnectionType.SqlServer)
                        _DbContext = GetSqlServerConnection();
                    else if (connectionType == enumConnectionType.MySql)
                        _DbContext = GetMySqlConnection();
                    _DbContext.Open();
                }
                return _DbContext;
            }
        }
        private static OracleConnection GetOracleConnection()
        {
            OracleConnection tempConn = new OracleConnection(OracleConnectionInfo);
            return tempConn;
        }

        private static SqlConnection GetSqlServerConnection()
        {
            SqlConnection tempConn = new SqlConnection(SqlServerConnectionInfo);
            return tempConn;
        }
        private static MySqlConnection GetMySqlConnection()
        {
            MySqlConnection tempConn = new MySqlConnection(MySqlConnectionInfo);
            return tempConn;
        }
        public void Dispose()
        {
            if (DbContext != null)
            {
                DbContext.Close();
                DbContext.Dispose();
            }
        }
    }
}
